//
//  YQTransferDetailVC.m
//  NewRetail
//
//  Created by yuqin on 2019/6/26.
//  Copyright © 2019 yuqin. All rights reserved.
//

#import "YQTransferDetailVC.h"
#import "YQTransferDetailCell.h"

@interface YQTransferDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *detailTB;
@property (nonatomic, strong) UILabel *promptLab;
@property (nonatomic, strong) UIButton *commitBtn;

@end

@implementation YQTransferDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.txtTitle.text = @"转账";
    self.view.backgroundColor = UIColorFromRGB(0xeeeeee);
    [self setupView];
}

- (void)setupView {
    [self.view addSubview:self.detailTB];
    [self.view addSubview:self.promptLab];
    [self.view addSubview:self.commitBtn];
    
    [self.detailTB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kMargin_left);
        make.top.mas_equalTo(kMargin_top+NavigationStatusBarHeight);
        make.right.mas_equalTo(-kMargin_right);
        make.height.mas_equalTo(FIT(300));
    }];
    
    [self.promptLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.equalTo(self.detailTB.mas_bottom).mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(ScreenWidth, FIT(40)));
    }];
    
    [self.commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kMargin_left);
        make.top.equalTo(self.promptLab.mas_bottom).mas_equalTo(FIT(20));
        make.size.mas_equalTo(CGSizeMake(ScreenWidth-2*kMargin_left, kCommitButtonHeight));
    }];
    
}

- (UITableView *)detailTB {
    if (!_detailTB) {
        _detailTB = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _detailTB.layer.cornerRadius = 5;
        _detailTB.separatorStyle = UITableViewCellSeparatorStyleNone;
        _detailTB.delegate = self;
        _detailTB.dataSource = self;
        [_detailTB registerClass:[YQTransferDetailCell class] forCellReuseIdentifier:YQ_TransferDetailCell];
    }
    return _detailTB;
}

- (UILabel *)promptLab {
    if (!_promptLab) {
        _promptLab = [YQViewFactory labelWithTextColor:MainRedTextColor textAlignment:NSTextAlignmentCenter fontSize:FIT(13) userBold:NO];
        _promptLab.text = @"提示：转账前请务必确认地址及币种信息无误，一旦转出不可恢复";
        _promptLab.adjustsFontSizeToFitWidth = YES;
    }
    return _promptLab;
}

- (UIButton *)commitBtn {
    if (!_commitBtn) {
        _commitBtn = [YQViewFactory buttonWithTitle:@"转账" titleColor:MainBlackColor fontSize:kNormalButtonFont userBold:NO target:self sel:@selector(commitTransferAction)];
        _commitBtn.layer.cornerRadius = 3;
        [_commitBtn setBackgroundColor:UIColorFromRGB(0xdfe2ea)];
        //当没有填充内容的时候背景为灰色 并且不能点击 或者可转余额不足时也为灰色
    }
    return _commitBtn;
}

#pragma mark - daili
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return FIT(60);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YQTransferDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:YQ_TransferDetailCell forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        [self changePlaceholderFontWithText:@"扫码或输入" font:FIT(13) textfield:cell.addressTF];
        [cell.collectionListBtn setHidden:NO];
        [cell.scanningBtn setHidden:NO];
        [cell.addressTF setHidden:NO];
    }else if (indexPath.row == 1) {
        
        [cell.addressLab setHidden:NO];
    }else if (indexPath.row == 2) {
        [self changePlaceholderFontWithText:@"输入转出金额" font:FIT(15) textfield:cell.addressTF];
        [cell.addressTF setHidden:NO];
        [cell.coinsNameLab setHidden:NO];
    }else if (indexPath.row == 3) {
        [cell.lineView setHidden:YES];
        [cell.rightBtn setHidden:NO];
        [cell.coinsNumsLab setHidden:NO];
        
        cell.addressLab.textAlignment = NSTextAlignmentRight;
    }else {
        [cell.lineView setHidden:YES];
        [cell.collectionListBtn setHidden:NO];
        [cell.coinsNumsLab setHidden:NO];
        //假值
        cell.coinsNumsLab.text = @"0.000345 WDC";
        //更改frame
        [cell.coinsNumsLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell.collectionListBtn.mas_left).mas_equalTo(-FIT(10));
        }];
        [cell.collectionListBtn setImage:YQ_IMAGE(@"why") forState:UIControlStateNormal];
    }
    
    NSDictionary *dict = @{@"1":@"收款地址",
                           @"2":@"转出地址",
                           @"3":@"转出金额",
                           @"4":@"",
                           @"5":@"交易费用"
                           };
    cell.theTitleLab.text = [dict objectForKey:StringFromLongInt(indexPath.row+1)];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark - 提交
- (void)commitTransferAction {
    
}

- (void)changePlaceholderFontWithText:(NSString *)placeHolder font:(CGFloat)font textfield:(UITextField *)textfield {
    NSString *holderText = placeHolder;
    NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
    [placeholder addAttribute:NSForegroundColorAttributeName
                        value:[UIColor lightGrayColor]
                        range:NSMakeRange(0, holderText.length)];
    [placeholder addAttribute:NSFontAttributeName
                        value:[UIFont systemFontOfSize:font]
                        range:NSMakeRange(0, holderText.length)];
    textfield.attributedPlaceholder = placeholder;
}

@end
